package com.holly.unit.deform.modular.controller;

import cn.hutool.core.bean.BeanUtil;
import com.holly.unit.core.pojo.request.BaseRequest;
import com.holly.unit.core.pojo.response.ErrorResponseData;
import com.holly.unit.core.pojo.response.ResponseData;
import com.holly.unit.core.pojo.response.SuccessResponseData;
import com.holly.unit.db.api.pojo.page.PageResult;
import com.holly.unit.deform.api.DeformUrlAuthApi;
import com.holly.unit.deform.entity.DesignFormUrlAuth;
import com.holly.unit.deform.modular.pojo.*;
import com.holly.unit.scanner.api.annotation.ApiResource;
import com.holly.unit.scanner.api.annotation.GetResource;
import com.holly.unit.scanner.api.annotation.PostResource;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 类描述: 表单设计器url授权服务 控制器
 *
 * @author fxr
 * @version 1.0.0
 * @date 2022/4/11 15:31
 */
@Slf4j
@RestController
@ApiResource(name = "表单设计器url授权服务")
@Api(tags = "表单设计器url授权服务")
public class DeformUrlAuthController {

    @Resource
    private DeformUrlAuthApi api;

    @ApiOperation(value = "查询所有状态" ,notes = "查询所有状态")
    @GetResource(name = "查询所有状态" , path = "/designFormUrlAuth/queryAllStatus")
    public ResponseData queryAllStatus(@Validated(DesignFormUrlAuthQueryRequest.queryByCode.class) DesignFormUrlAuthQueryRequest request) {
        List<DesignFormUrlAuth> list = api.fetchAllStatusByDesformCode(request.getDesformCode());
        return new SuccessResponseData(list);
    }

    /** 修改所以的状态 */
    @ApiOperation(value = "修改所以的状态" ,notes = "修改所以的状态")
    @PostResource(name = "修改所以的状态" , path = "/designFormUrlAuth/editAllStatus")
    public ResponseData editAllStatus(@RequestBody DesignFormUrlAuthEditStatusRequest request) {
        String desformCode = request.getDesformCode();
        Integer add = request.getAdd();
        Integer edit = request.getEdit();
        Integer detail = request.getDetail();
        return new SuccessResponseData(api.setAllUrlStatus(desformCode, add, edit, detail));
    }

    /**
     * 分页列表查询
     *
     * @param designFormUrlAuth
     * @param req
     * @return
     */
    @ApiOperation(value = "分页列表查询" ,notes = "分页列表查询")
    @GetResource(name = "分页列表查询" , path = "/designFormUrlAuth/queryPageList")
    public ResponseData queryPageList(DesignFormUrlAuth designFormUrlAuth, HttpServletRequest req) {
        PageResult<DesignFormUrlAuth> page = api.page(designFormUrlAuth, req.getParameterMap());
        return new SuccessResponseData(page);
    }

    /**
     * 批量删除
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "批量删除" ,notes = "批量删除")
    @PostResource(name = "批量删除" , path = "/designFormUrlAuth/deleteBatch")
    public ResponseData deleteBatch(@Validated(DesignFormDelRequest.delByIds.class) DesignFormDelRequest request) {
        api.delBatch(request.getIds());
        return new SuccessResponseData();
    }

    /**
     * 通过id查询
     *
     * @param request
     * @return
     */
    @ApiOperation(value = "通过id查询" ,notes = "通过id查询")
    @GetResource(name = "通过id查询" , path = "/designFormUrlAuth/queryById")
    public ResponseData queryById(@Validated(DesignFormQueryByIdRequest.queryById.class) DesignFormQueryByIdRequest request) {
        DesignFormUrlAuth designFormUrlAuth = (DesignFormUrlAuth)api.fetchEntityById(request.getId());
        return new SuccessResponseData(designFormUrlAuth);
    }

    /**
     * 新增
     *
     * @param designFormUrlAuthRequest
     * @return
     */
    @ApiOperation(value = "_增加" ,notes = "_增加")
    @PostResource(name = "_增加", path = "/designFormUrlAuth/add")
    public ResponseData add(@RequestBody @Validated(BaseRequest.add.class) DesignFormUrlAuthRequest designFormUrlAuthRequest) {
        DesignFormUrlAuth designFormUrlAuth = new DesignFormUrlAuth();
        BeanUtil.copyProperties(designFormUrlAuthRequest,designFormUrlAuth);
        api.saveEntity(designFormUrlAuth);
        return new SuccessResponseData();
    }
    /**
     * 编辑
     * @param designFormUrlAuthRequest
     * @author holly
     * @date 2022-03-03 03:40:09
     */
    @ApiOperation(value = "_编辑" ,notes = "_编辑")
    @PostResource(name = "_编辑", path = "/designFormUrlAuth/edit")
    public ResponseData edit(@RequestBody @Validated(BaseRequest.edit.class) DesignFormUrlAuthRequest designFormUrlAuthRequest) {
        DesignFormUrlAuth designFormUrlAuth = new DesignFormUrlAuth();
        BeanUtil.copyProperties(designFormUrlAuthRequest,designFormUrlAuth);
        boolean b = api.editEntity(designFormUrlAuth);
        if (b) {
            return new SuccessResponseData();
        } else {
            return new ErrorResponseData("500","编辑出错");
        }
    }
}
